﻿using System;
using System.IO;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.Security;
using FranceTriple.Dal;
using TeamFranceTriple.Config;

namespace TeamFranceTriple
{
    public static class MailManager
    {
        private static string welcomeTftLocalized = "WelcomeTft".ToLocalizedString();

        public static void SendEmail(MailAddress from, MailAddress to, String subject, String Body, Boolean hasToSendCopyToAdmin)
        {
            MailMessage mm = null;
            SmtpClient sc = null;
            try
            {
                mm = new MailMessage(from, to);
                if (hasToSendCopyToAdmin)
                    foreach (var user in Roles.GetUsersInRole("RoleAdminForum"))
                        mm.Bcc.Add(new MailAddress(Membership.GetUser(user, false).Email));

                mm.IsBodyHtml = true;
                mm.BodyEncoding = Encoding.UTF8;

                //FRO : il faut enlever les saut de ligne '\r\n' du sujet de l'email car cela lève une exception
                mm.Subject = subject.Replace("\r\n", " ");

                mm.Body = Body;

                sc = new SmtpClient(TeamFranceTripleConfig.Smtp.Network.Host, TeamFranceTripleConfig.Smtp.Network.Port);
                sc.EnableSsl = TeamFranceTripleConfig.Smtp.Network.EnableSsl;
                sc.UseDefaultCredentials = TeamFranceTripleConfig.Smtp.Network.DefaultCredentials;
                if (!sc.UseDefaultCredentials)
                    sc.Credentials = new System.Net.NetworkCredential(TeamFranceTripleConfig.Smtp.Network.UserName, TeamFranceTripleConfig.Smtp.Network.Password, TeamFranceTripleConfig.Smtp.Network.ClientDomain);
                sc.Send(mm);
            }
            catch (Exception ex)
            {
                LoggingManager.Logger.Log(LoggingCategory.Error, ex.Message);
                throw new DalException(String.Format("InsertError".ToLocalizedString(), "SmtpClient", ex.Message), ex) { EntityType = typeof(SmtpClient) };
            }
            finally
            {
                if (sc != null)
                    sc.Dispose();
            }
        }

        public static void SendActivationEmail(String UserName, String TemplatePathAndName, Boolean hasToSendCopyToAdmin)
        {
            var user = Membership.GetUser(UserName);
            if (user == null)
                throw new HttpException(404, "UserNotFound".ToLocalizedString() + " : " + UserName);

            String MailBody = File.ReadAllText(TemplatePathAndName);
            MailBody = String.Format(MailBody, user.UserName, user.ProviderUserKey.ToString());

            MailManager.SendEmail(
                new MailAddress(TeamFranceTripleConfig.WebConfig.mailerror.from.ToString()),
                new MailAddress(user.Email),
                welcomeTftLocalized + " " + user.UserName,
                MailBody,
                hasToSendCopyToAdmin);
        }
    }
}
